package com.tisson.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.tisson.bo.DepartmentBO;
import com.tisson.bo.FilterBO;
import com.tisson.bo.UserBO;
import com.tisson.comment.WebConstants;
import com.tisson.pojo.AcUser;
import com.tisson.pojo.Page;

public class UserAction extends ActionSup{
	
	private String userId;
	private String callId;
	private String workerNO;
	private Date expiryDate;
	private String userDepartment;
	private String username;
	private String password;
	private String userPhone;
	private String userEmail;
	private Integer userState;
	private List result;
	private List deptList;
	private List stateList;
	private Page page;
	private String id;
	private String logicMark0;
	private String logicMark1;
	private String logicMark2;
	private String logicMark3;
	
	public String search(){
		UserBO bo = new UserBO();
		page = new Page();
		List all;
		result = new ArrayList();
		try{
			all = bo.search(request,workerNO, username, callId, userDepartment, userState, logicMark0, logicMark1, logicMark2, logicMark3);
		}
		catch(Exception e){
			e.printStackTrace();
			all = new ArrayList();
		}
		request.getSession().setAttribute("list", all);
		try{
			for(int i=0;i<WebConstants.PAGESIZE;i++){
				if(all.get(i)!=null){
					result.add(all.get(i));
				}
			}
		}catch(Exception e){
		}
		page.setCurrentPage(1);
		page.setPrePage(page.getCurrentPage()-1);
		page.setNextPage(page.getCurrentPage()+1);
		page.setListSize(all.size());
		page.setTotalPages(page.getListSize()%WebConstants.PAGESIZE==0?page.getListSize()/WebConstants.PAGESIZE:(page.getListSize()/WebConstants.PAGESIZE)+1);
		return "list";
	}
	
	public String list(){
		UserBO bo = new UserBO();
		page = new Page();
		List all;
		result = new ArrayList();
		try{
			all = bo.findAll(request);
		}
		catch(Exception e){
			e.printStackTrace();
			all = new ArrayList();
		}
		request.getSession().setAttribute("list", all);
		try{
			for(int i=0;i<WebConstants.PAGESIZE;i++){
				if(all.get(i)!=null){
					result.add(all.get(i));
				}
			}
		}catch(Exception e){
		}
		page.setCurrentPage(1);
		page.setPrePage(page.getCurrentPage()-1);
		page.setNextPage(page.getCurrentPage()+1);
		page.setListSize(all.size());
		page.setTotalPages(page.getListSize()%WebConstants.PAGESIZE==0?page.getListSize()/WebConstants.PAGESIZE:(page.getListSize()/WebConstants.PAGESIZE)+1);
		return "list";
	}
	
	public String delete(){
		UserBO bo = new UserBO();
		String[] idList = request.getParameterValues("id");
		bo.delete(idList);
		return list();
	}
	
	public String add(){
		UserBO bo = new UserBO();
		DepartmentBO deptBO = new DepartmentBO();
		result = deptBO.findAll(request);
		return "addView";
	}
	
	public String save(){
		UserBO bo = new UserBO();
		AcUser user = new AcUser();
		user.setCallId(callId);
		user.setExpiryDate(expiryDate);
		user.setPassword(password);
		user.setUserDepartment(userDepartment);
		user.setUserEmail(userEmail);
		user.setUsername(username);
		user.setUserPhone(userPhone);
		user.setUserState(userState);
		user.setWorkerNO(workerNO);
		bo.add(user);
		return list();
	}
	
	public String edit(){
		try{
			UserBO bo = new UserBO();
			AcUser user = bo.findById(id);
			DepartmentBO deptBO = new DepartmentBO();
			deptList = deptBO.findAll(request);
			stateList = bo.getStateList();
			userId = user.getUserId();
			callId = user.getCallId();
			workerNO = user.getWorkerNO();
			expiryDate = user.getExpiryDate();
			userDepartment = user.getUserDepartment();
			username = user.getUsername();
			password = user.getPassword();
			userPhone = user.getUserPhone();
			userEmail = user.getUserEmail();
			userState = user.getUserState();
			
		}
		catch(Exception e){
			
		}
		return "editView";
	}
	
	public String update(){
		try{
			UserBO bo = new UserBO();
			AcUser user = new AcUser();
			user.setCallId(callId);
			user.setExpiryDate(expiryDate);
			user.setPassword(password);
			user.setUserDepartment(userDepartment);
			user.setUserEmail(userEmail);
			user.setUserId(userId);
			user.setUsername(username);
			user.setUserPhone(userPhone);
			user.setUserState(userState);
			user.setWorkerNO(workerNO);
			bo.update(user);
			
		}catch(Exception e){
		}
		return list();
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getCallId() {
		return callId;
	}

	public void setCallId(String callId) {
		this.callId = callId;
	}

	public String getWorkerNO() {
		return workerNO;
	}

	public void setWorkerNO(String workerNO) {
		this.workerNO = workerNO;
	}

	public Date getExpiryDate() {
		return expiryDate;
	}

	public void setExpiryDate(Date expiryDate) {
		this.expiryDate = expiryDate;
	}

	public String getUserDepartment() {
		return userDepartment;
	}

	public void setUserDepartment(String userDepartment) {
		this.userDepartment = userDepartment;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserPhone() {
		return userPhone;
	}

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public Integer getUserState() {
		return userState;
	}

	public void setUserState(Integer userState) {
		this.userState = userState;
	}

	public List getResult() {
		return result;
	}

	public void setResult(List result) {
		this.result = result;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLogicMark0() {
		return logicMark0;
	}

	public void setLogicMark0(String logicMark0) {
		this.logicMark0 = logicMark0;
	}

	public String getLogicMark1() {
		return logicMark1;
	}

	public void setLogicMark1(String logicMark1) {
		this.logicMark1 = logicMark1;
	}

	public String getLogicMark2() {
		return logicMark2;
	}

	public void setLogicMark2(String logicMark2) {
		this.logicMark2 = logicMark2;
	}

	public String getLogicMark3() {
		return logicMark3;
	}

	public void setLogicMark3(String logicMark3) {
		this.logicMark3 = logicMark3;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

	public List getStateList() {
		return stateList;
	}

	public void setStateList(List stateList) {
		this.stateList = stateList;
	}
	
	

}
